package com.cy.kongjian;

import com.carrotsearch.sizeof.RamUsageEstimator;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import org.openjdk.jol.info.GraphLayout;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

public class KongJianTest {

	@Test
	public void t1() {
		LoadingCache<Long, Long> cache = Caffeine.newBuilder()
				.maximumSize(100)
				.expireAfterAccess(1, TimeUnit.DAYS)
				.recordStats()
				.build(key -> key);
		System.out.println(RamUsageEstimator.sizeOf(cache));

		for (long i = 0; i < 100; i++) {
			Long aLong = cache.get(i);
			Assert.assertEquals(i, (long) aLong);
		}
		System.out.println(RamUsageEstimator.sizeOf(cache));

		for (long i = 0; i < 10000000; i++) {
			Long aLong = cache.get(i);
			Assert.assertEquals(i, (long) aLong);
		}
		System.out.println(RamUsageEstimator.sizeOf(cache));


		//查看对象内部信息
		// System.out.println(ClassLayout.parseInstance(cache).toPrintable());
		System.out.println("----------我是分割线---------------");
		//查看对象外部信息
		// System.out.println(GraphLayout.parseInstance(cache).toPrintable());
		System.out.println("----------我是分割线---------------");

		//获取对象总大小
		System.out.println("size : " + GraphLayout.parseInstance(cache).totalSize());

	}
}
